package com.windgo.config;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.Data;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.util.Map;


/**
 * @author 张林浩
 * @since 2022/10/16 16:52
 */
@Component
@ConfigurationProperties(prefix = "aliyun")
@Data
public class AliyunComponent {

    private String accessKeyId = "LTAI5t8gpxPTCR6W58RnZq4u";
    private String accessKeySecret = "mVPbpbxia0JQotb7HyJAREV8QUuq8h";

    Oss oss;
    Sms sms;

    public OSSClient getOssClient() {
        OSSClient ossClient = new OSSClient(oss.getEndPoint(), accessKeyId, accessKeySecret);
        return ossClient;
    }

    /**
     * 上传文件模板
     * <p>
     * 调用方法时，使用自动注入AliyunComponent,然后aliyunComponent.fileUpload(fileName,file)即可
     */
    public void fileUpload(String fileName, File file) {
        OSSClient ossClient = getOssClient();
        PutObjectResult putObjectResult = ossClient.putObject(oss.getBucket(), fileName, file);
    }


    public void fileUpload(String fileName, InputStream inputStream) {
        OSSClient ossClient = getOssClient();
        PutObjectResult putObjectResult = ossClient.putObject(oss.getBucket(), fileName, inputStream);
    }

    /**
     * 发送验证码
     * @param phoneNumber
     * @param code
     */
    public void sendMsg(String phoneNumber, String code) {

        // DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        // IAcsClient client = new DefaultAcsClient(profile);
        // CommonRequest request = new CommonRequest();
        // request.setSysMethod(MethodType.POST);
        // request.setSysDomain("dysmsapi.aliyuncs.com"); //云服务(不能修改)
        // request.setSysVersion("2017-05-25"); //版本号(不能修改)
        // request.setSysAction("SendSms"); // 发送方式(不能修改)
        // request.putQueryParameter("RegionId", "cn-hangzhou");
        // // 发送的手机号（多个手机号可以用逗号隔开）
        // request.putQueryParameter("PhoneNumbers", phoneNumber);
        // request.putQueryParameter("SignName", "stone4j");
        // // 3.请求模板(短信模板)
        // request.putQueryParameter("TemplateCode", "SMS_173765187");
        // // 4.请求模板参数（需要时json数组格式，验证码可以使用随机参数）
        // request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
        // CommonResponse response = null;
        // try {
        //     response = client.getCommonResponse(request);
        //     System.out.println(response.getData());
        // } catch (ClientException e) {
        //     e.printStackTrace();
        // }
      //  String phoneNumber = map.get("mobile");
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        request.putQueryParameter("PhoneNumbers", phoneNumber);


        request.putQueryParameter("SignName", "stone4j");
        request.putQueryParameter("TemplateCode", "SMS_173765187");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println(response.getData());



    }


}
